<?php
class ManagePromotionHotelAddForm extends Form
{
    function ManagePromotionHotelAddForm()
    {
        Form::Form("ManagePromotionHotelAddForm");
        $this->link_css("style/hotel.css");
        $this->link_css('style/jquery-ui-1.9.2.custom.css');
        $this->link_css('style/msgBoxLight.css');
        $this->link_js("javascript/jquery/jquery.ui.core.js");
        $this->link_js("javascript/jquery/jquery.ui.widget.js");
        $this->link_js("javascript/jquery/jquery.ui.mouse.js");
        $this->link_js("javascript/jquery/jquery.ui.sortable.js");
        $this->link_js("javascript/jquery/jquery.ui.sortable.js");
        $this->link_js("javascript/jquery/jquery.ui.datepicker.js");
        //
    }

    function on_submit()
    {
        $promotion['hotel_id'] = Url::getParamInt('order', 0);
        $promotion['room_id'] = Url::getParamInt('room_hotel', 0);
        $promotion['type'] = Url::getParamInt('type', 0);
        $promotion['time_c'] = time();

        $promotion['time_start'] = Url::get('time_start', 0);
        $a = explode("-", $promotion['time_start']);
        $promotion['time_start'] = strtotime($a[1] . "/" . $a[0] . "/" . $a[2]);

        $promotion['time_end'] = Url::get('time_end', 0);
        $b = explode("-", $promotion['time_end']);
        $promotion['time_end'] = strtotime($b[1] . "/" . $b[0] . "/" . $b[2]);

        $promotion['price'] = (int)Url::get('price', 0);
        $promotion['percent'] = (int)Url::get('percent', 0);
        $promotion['status'] = 1;
        $promotion['user_c'] = User::id();


        $valid = true;
        if ($promotion['price'] == '') {
            $valid = false;
            $this->setFormError("error_name", "Giá khuyến mãi không được để trống!");
        }

        if ((int)Url::get('price', 0) === 1) {
            if ($promotion['percent'] < 1 || $promotion['percent'] > 99) {
                $valid = false;
                $this->setFormError("error_percent", "Phần trăm khuyến mãi nhập không đúng!");
            }
        }
        if (Db_Promotion::checkpromotion($promotion) != 0) {
            $valid = false;
            $this->setFormError("error_percent", "Trùng thời gian và Loại với khuyến mãi đã có !");
        }

        if ($valid) {
            if ($idPromotion = Db_Promotion::insertPromotionHotel($promotion)) { //Thanh cong
                $this->setFormSucces("success", "Save hành công!");
                Url::redirect_current();
            } else {
                $this->setFormError("message", "Có lỗi xảy ra khi thêm vào database.");
            }
        }
    }

    function draw()
    {
        global $display;
        $display->add("WEB_ROOT", WEB_ROOT);
        $display->add("WEB_DIR", WEB_DIR);
        $this->beginForm(true);
//        $hotel = Db_Promotion::showDataHotel();
//        $hotel = array();
//        $display->add("hotel_cities", $hotel);
//        $hotel_id = array_keys($hotel);
//        $room = array();
//        foreach ($hotel_id as $hid) {
//            $room[$hid] = Db_Room::getRoomsByHotelId($hid);
//        }
//        $display->add("room", $room);

        $display->add("type", Db_Promotion::showDataFilter());


        $display->add("title", "Thêm mới khuyến mãi khách sạn");
        $display->add("title_submit", "Thêm mới");
        $display->add("message", $this->showFormErrorMessages(1));
        $display->add("success", $this->showFormSuccesMessages(1));

        $display->output("PromotionHotelForm");

        $this->endForm();
    }

    function get_data_checkbox()
    {
        $features_checkbox_name = array();
        $features = array();
        $features_group = Db_Filter::getHotelFilersGroup();

        foreach ($features_group as $value) {
            $id = $value['id'];
            $features_temp = Db_Filter::getHotelFilersByGroupId($id);
            foreach ($features_temp as $v) {
                $features[$id][$v['id']] = $v['name'];
            }
            $features_checkbox_name[$id] = "chb_features_" . $id;
        }

        return array("features_group" => $features_group, "features_checkbox_name" => $features_checkbox_name, "features" => $features);
    }
}

?>